/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.servlet;

import aptech.container.Constants;
import aptech.data.ActivityBO;
import aptech.data.JoinActivityBO;
import aptech.data.PaymentBO;
import aptech.data.UserBO;
import aptech.entities.Activity;
import aptech.entities.UserJoinToActivity;
import aptech.entities.Users;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author home
 */
@WebServlet(name = "JoinToActivityServlet", urlPatterns = {"/JoinToActivityServlet"})
public class JoinToActivityServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String type = request.getParameter(Constants.TYPE);
            String action = request.getParameter(Constants.ACTION);
            if (type.equals(Constants.JOIN_ACTIVITY)) {
                if (action.equals(Constants.ACTION_ADD)) {
                    //check user
                    HttpSession session = request.getSession();
                    Users user = (Users) session.getAttribute(Constants.LOGIN);
                    if (user != null) {
                        //update current people
                        int userid = user.getUserId();//request.getParameter(Constants.USER_ID);
                        String activityid = request.getParameter(Constants.ACTIVITY_ID);
                        String paymentid = request.getParameter(Constants.PAYMENT_ID);
                        String status = request.getParameter(Constants.JOIN_PAYMENT_STATUS);
                        //default
                        if (activityid == null) {
                            activityid = "1";
                        }
                        if (paymentid == null) {
                            paymentid = "1";
                        }
                        if (status == null) {
                            status = "True";
                        }
                        UserJoinToActivity userjoin = new UserJoinToActivity();
                        userjoin.setActivity(new ActivityBO().getById(Integer.parseInt(activityid), true));
                        userjoin.setPayment(new PaymentBO().getById(Integer.parseInt(paymentid), true));
                        userjoin.setUsers(new UserBO().getById(userid, true));
                        userjoin.setStatusPayment(status);

                        Activity activity = new ActivityBO().getById(Integer.parseInt(activityid), true);
                        activity.setCurrentPeople(activity.getCurrentPeople().intValue()+1);
                        //save
                        boolean kq = new JoinActivityBO().addNew(userjoin);
                        boolean kq2 = new ActivityBO().update(activity);
                        
                        if (kq && kq2) {
                            out.println("<result>success</result>");
                        }
                    }else{
                        out.println("<result>null</result>");
                    }
                }

                if (action.equals(Constants.ACTION_EDIT)) {
                    //do sth
                }
                if (action.equals(Constants.ACTION_DELETE)) {
                    //do sth
                }
            }
        } catch (Exception ex) {
            out.println("<result>error</result>");
            ex.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
